package com.jzg.jcptadmin.ui;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.TextPaint;
import android.util.DisplayMetrics;
import android.view.KeyEvent;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;

import com.jzg.jcptadmin.R;
import com.jzg.jcptadmin.base.BaseActivity;
import com.jzg.jcptadmin.constant.Constant;

/**
 * Function: 单选界面 <br/>
 * Date: 2015-4-29 9:30:36 <br/>
 * 
 * @author 郑有权
 * @version
 * @since JDK 1.6
 * @see
 */
@SuppressLint("ResourceAsColor")
public class RadioActivity1 extends BaseActivity {

	TextView radio_title;

	int requestCode;

	RadioGroup radioGroup_traddition;


	private String[] currentRadioGroup;
	private String currentCheck;
	private String currentTitle;
	
	
	int currentCheckPos = -1;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		//Log.i("Radio", "onCreate");
		setContentView(R.layout.activity_radio1);
		radio_title = (TextView) findViewById(R.id.radio_title);

		radioGroup_traddition = (RadioGroup) findViewById(R.id.radioGroup_traddition);
		requestCode = getIntent().getIntExtra("requestCode", 0);
		currentCheckPos = getIntent().getIntExtra(Constant.activity_radio_pos, -1);
		
		currentRadioGroup = getIntent().getStringArrayExtra(Constant.activity_radioGroup);
		currentTitle = getIntent().getStringExtra(Constant.activity_radio_title);
		
		radio_title.setText(currentTitle);
		
		setRadio();

	}

	@SuppressLint("NewApi")
	public void setRadio() {
		for (int i = 0; i < currentRadioGroup.length; i++) {

			Drawable drawable = this.getResources().getDrawable(
					R.drawable.radio);
			drawable.setBounds(0, 0, drawable.getIntrinsicWidth(),
					drawable.getIntrinsicHeight());

			RadioButton radio = new RadioButton(getApplicationContext());
			radio.setBackgroundResource(R.drawable.radio_bj);
			/*Drawable background = this.getResources().getDrawable(
					R.drawable.btn_blue);
			radio.setBackground(background);*/
			if(i == currentCheckPos){
				radio.setChecked(true);
			}
			
			radio.setText(currentRadioGroup[i]);
			//radio.setBackgroundColor(R.color.red);
			//float w =  GetTextWidth(currentRadioGroup[i], 20);
			
			DisplayMetrics  dm = new DisplayMetrics();     
			getWindowManager().getDefaultDisplay().getMetrics(dm);     
			int screenWidth = dm.widthPixels;               
			int screenHeight = dm.heightPixels;
			//Log.i("RadioActivity", "screenWidth-------" + screenWidth);
			//radio.setWidth(screenWidth-100);
			//radio.setHeight(180);
			radio.setTextAppearance(appContext, R.style.small_grey_text_style);
			//radio.setTextColor(this.getResources().getColor(R.color.tradition_item_text_color));
			radio.setPadding(0, 0, 50, 0);
			radio.setButtonDrawable(android.R.color.transparent);
			/*LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
					LinearLayout.LayoutParams.WRAP_CONTENT,
					LinearLayout.LayoutParams.WRAP_CONTENT);*/
			//radio.setCompoundDrawablePadding(200);
			radio.setCompoundDrawables(null, null, drawable, null);
			radio.setId(i);
			radioGroup_traddition.addView(radio);
		}
		radioGroup_traddition
				.setOnCheckedChangeListener(new OnCheckedChangeListener() {

					@Override
					public void onCheckedChanged(RadioGroup group, int checkedId) {
						//Log.i("RadioActivity", "checkedId-------" + checkedId);

						//Log.i("RadioActivity", "选中-------"+ currentRadioGroup[checkedId]);

						currentCheck = currentRadioGroup[checkedId];
						currentCheckPos = checkedId;
						data_back();
					}

				});
	}
	
	// 第一个参数是要计算的字符串，第二个参数是字提大小
	public static float GetTextWidth(String text, float Size) {
		TextPaint FontPaint = new TextPaint();
		FontPaint.setTextSize(Size);
		return FontPaint.measureText(text);
	}

	/**
	 * 返回
	 * 
	 * @param v
	 */
	public void act_radio_top_back(View v) {
		this.finish();
	}
	@Override
	public void onBackPressed() {
		// TODO Auto-generated method stub
		super.onBackPressed();
		this.finish();
	}
	
	

	
	
	public void data_back(){
			Intent mIntent = new Intent();
			mIntent.putExtra(Constant.activity_radio, currentRadioGroup[currentCheckPos]);
			mIntent.putExtra(Constant.activity_radio_pos, currentCheckPos);
			this.setResult(requestCode, mIntent);
			this.finish();
	}

	@Override
	protected void onPause() {
		super.onPause();
		//Log.i("RadioActivity", "onPause");
	}
	
	@Override
	protected void onStop() {
		super.onStop();
		//Log.i("RadioActivity", "onStop");
	}
	@Override
	protected void onDestroy() {
		super.onDestroy();
		//Log.i("RadioActivity", "onDestroy");
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {

		if (keyCode == KeyEvent.KEYCODE_BACK) {
			data_back();
			this.finish();
		} 
		return false;
	}
	

}
